//
//  ChongZhiViewController.m
//  1243
//
//  Created by 王 on 16/5/16.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ChongZhiViewController.h"
#import "NetWorkTools.h"

@interface ChongZhiViewController ()<UIAlertViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *GongSiZhangHuName;

@property (weak, nonatomic) IBOutlet UITextField *userId;
@property (weak, nonatomic) IBOutlet UITextField *money;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *conpanyId;

@property (nonatomic, strong) NSString *phoneNum;

@property (weak, nonatomic) IBOutlet UITextField *bankName;

@end

@implementation ChongZhiViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getBankInfoData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"充值";
    
    self.userId.delegate = self;
    self.money.delegate = self;
    self.pwd.delegate = self;
    self.userName.delegate = self;
    self.conpanyId.delegate = self;
    self.GongSiZhangHuName.delegate =self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadUserName) name:UITextFieldTextDidChangeNotification object:self.userId];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    //    NSLog(@"%@", textField.text);
}

// 键盘遮挡
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 100024|| textField.tag == 100023|| textField.tag == 100025) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}
- (void)loadUserName {
    self.phoneNum = self.userId.text;
    NSLog(@"%@",self.phoneNum);
    if (self.userId.text.length == 11) {
        [self getSearchUserData];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   
    if (string.length == 0) return YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    
    if (existedLength - selectedLength + replaceLength >= 20) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.userId) {
        if (textField.text.length >=20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)chongZhi:(UIButton *)sender {
    
    if ([self.pwd.text length] && [self.money.text length] && [self.conpanyId.text length]) {
     
        if ([self.userId.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
            [alert show];
            return;
        } else if ([self.money.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入充值金额" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
            [alert show];
            return;
        } else if ([self.pwd.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入交易密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
            [alert show];
            return;
        } else if ([self.userName.text isEqualToString:@""]) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
            [alert show];
        }
        
        if ([self.payPassword isEqualToString:self.pwd.text]) {
        
            NSDictionary *dict = @{
                                   @"UserId":self.userId.text,
                                   @"RechargeMoney":self.money.text,
                                   @"BusPassword":self.pwd.text,
                                   @"ReAccountType":@"0"
                                   };
            
            [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"RECHARGEACCOUNT" completeBlock:^(id object) {
                self.userId.text = @"";
                self.money.text = @"";
                self.pwd.text = @"";
                self.userName.text = @"";
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message: object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
                alert.tag = 12345;
                [alert show];
                
                NSLog(@"%@",object[@"Message"]);
            }];
        }else{
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您输入的交易密码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
            [alert show];
            
        }
 
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 12345) {
        self.pwd.text = @"";
        self.money.text = @"";
    }
}
 

- (void)getSearchUserData
{
    NSDictionary *dict = @{
                           @"Number":[NSString stringWithFormat:@"%@",self.phoneNum]
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"NUMUSERQUERY" completeBlock:^(id object) {
        NSLog(@"%@",object)
        
        if ([object[@"Message"] isEqualToString:@"查询成功"]) {
            NSString *DataString = [object objectForKey:@"BackData"];
            
            NSArray *array = [NetWorkTools arrayWithJsonString:DataString];
            
            NSDictionary *UserDict= [array firstObject];
            self.userName.text = [NSString stringWithFormat:@"%@",UserDict[@"RealName"]];
        }
        
    }];
}

- (void)getBankInfoData
{
    NSDictionary *dict = @{
                           @"MenthodParms":@""
                               
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"QUERYBANKINFO" completeBlock:^(id object) {
        NSLog(@"%@",object)
        
        if ([object[@"Id"] isEqual:@(0)]) {
            NSString *DataString = [object objectForKey:@"BackData"];
            
            NSArray *array = [NetWorkTools arrayWithJsonString:DataString];
            
            NSDictionary *bankDict= [array firstObject];
            self.GongSiZhangHuName.text =[NSString stringWithFormat:@"%@",bankDict[@"CompanyName"]];
            self.conpanyId.text = [NSString stringWithFormat:@"%@",bankDict[@"BankCode"]];
//            BankName
            self.bankName.text = [NSString stringWithFormat:@"%@",bankDict[@"BankName"]];
        }
        
    }];
}
- (void)registerLogInTextField {
    [self.userId resignFirstResponder];
    [self.money resignFirstResponder];
    [self.pwd resignFirstResponder];
    [self.userName resignFirstResponder];
    [self.conpanyId resignFirstResponder];
    
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerLogInTextField];
}


@end
